package com.mobilg.dating;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DbAdapter{
	
	private static final String PROFILES_TABLE_NAME = "profiles";
    private static final String PROFILES_ID = "_id";
    private static final String PROFILES_BIRTH_YEAR = "birthyear";
    private static final String PROFILES_BIRTH_MONTH = "birthmonth";
    private static final String PROFILES_BIRTH_DAY = "birthday";
    private static final String PROFILES_FIRSTNAME = "firstname";
    private static final String PROFILES_EMAIL = "email";
    private static final String PROFILES_PASSWORD = "password";
    private static final String PROFILES_LATITUDE = "lat";
    private static final String PROFILES_LONGITUDE = "lon";
    private static final String PROFILES_GENDER = "gender";
    private static final String PROFILES_SEARCHED_GENDER = "searched_gender";
    
    
	private Context context;
	private SQLiteDatabase database;
	private ChattingDBHelper dbHelper;
  

    public DbAdapter(Context ctx) {		
		this.context = ctx;
	}
		       	
	  /**
     * Open the profiles database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
	
    public DbAdapter open()throws SQLException {    	
        dbHelper = new ChattingDBHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
    }

    

	public void close() {
    	dbHelper.close();
    }


    /**
     * Create a new profile using the title and body provided. If the note is
     * successfully created return the new rowId for that note, otherwise return
     * a -1 to indicate failure.
     * 
     * @param title the title of the note
     * @param body the body of the note
     * @return rowId or -1 if failed
     */
    public long createProfile(String birthyear, String birthmonth, String birthday, String firstname, String email, String password, double latitude, double longitude,
    		String gender, String searchedGender) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(PROFILES_BIRTH_YEAR, birthyear);
        initialValues.put(PROFILES_BIRTH_MONTH, birthmonth);
        initialValues.put(PROFILES_BIRTH_DAY, birthday);
        initialValues.put(PROFILES_FIRSTNAME, firstname);
        initialValues.put(PROFILES_EMAIL, email);
        initialValues.put(PROFILES_PASSWORD, password);
        initialValues.put(PROFILES_LATITUDE, latitude);
        initialValues.put(PROFILES_LONGITUDE, longitude);
        initialValues.put(PROFILES_GENDER, gender);
        initialValues.put(PROFILES_SEARCHED_GENDER, searchedGender);

        return database.insert(PROFILES_TABLE_NAME, null, initialValues);
    }

    /**
     * Delete the note with the given rowId
     * 
     * @param rowId id of note to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteProfile(long rowId) {

        return database.delete(PROFILES_TABLE_NAME, PROFILES_ID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllProfiles() {

        return database.query(PROFILES_TABLE_NAME, new String[] {PROFILES_ID, PROFILES_BIRTH_YEAR, PROFILES_BIRTH_MONTH, PROFILES_BIRTH_DAY,
        		PROFILES_FIRSTNAME, PROFILES_EMAIL, PROFILES_PASSWORD, PROFILES_LATITUDE, PROFILES_LONGITUDE}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchProfile(String email) throws SQLException {

        Cursor mCursor =
        	database.query(true, PROFILES_TABLE_NAME, new String[] {
        			PROFILES_ID,
            	    PROFILES_BIRTH_YEAR, 
            	    PROFILES_BIRTH_MONTH, 
            	    PROFILES_BIRTH_DAY, 
            	    PROFILES_FIRSTNAME, 
            	    PROFILES_EMAIL, 
            	    PROFILES_PASSWORD, 
            	    PROFILES_LATITUDE, 
            	    PROFILES_LONGITUDE,
            	    PROFILES_GENDER,
            	    PROFILES_SEARCHED_GENDER}, 
            		PROFILES_EMAIL + "=" + "'"+email+"'", null, null, null, null, null);
        
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        
        return mCursor;

    }

    /**
     * Update the note using the details provided. The note to be updated is
     * specified using the rowId, and it is altered to use the title and body
     * values passed in
     * 
     * @param rowId id of note to update
     * @param title value to set note title to
     * @param body value to set note body to
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateProfile(long rowId, String birthyear, String birthmonth, String birthday, String firstname, String email, String password, double latitude, double longitude,
    		String gender, String searchedGender) {
        ContentValues args = new ContentValues();
        args.put(PROFILES_BIRTH_YEAR, birthyear);
        args.put(PROFILES_BIRTH_MONTH, birthmonth);
        args.put(PROFILES_BIRTH_DAY, birthday);
        args.put(PROFILES_FIRSTNAME, firstname);
        args.put(PROFILES_EMAIL, email);
        args.put(PROFILES_PASSWORD, password);
        args.put(PROFILES_LATITUDE, latitude);
        args.put(PROFILES_LONGITUDE, longitude);
        args.put(PROFILES_GENDER, gender);
        args.put(PROFILES_GENDER, searchedGender);
        
        return database.update(PROFILES_TABLE_NAME, args, PROFILES_ID + "=" + rowId, null) > 0;
    }

}
